<?php
declare(strict_types=1);

namespace Service\UserService;
use Service\BaseService;
use App\Model\BbcModel\Member;
use Service\Code\Normal;
use App\Model\BbcModel\MemberInfo;
use App\Model\BbcModel\MemberGuideLog;
use App\Model\BbcModel\ShoppingGuide;

/**
 * 会员中心
 * @author chenshubo
 */
class MemberService extends BaseService
{
    /**
     * 创建账户(所有新增用户(后台添加、用户注册)都走这里创建) 非常重要
     * @param int $cid
     * @param int $mn_sid
     * @param string $client
     * @param array $param
     */
    public function addUser(int $cid,int $mn_sid,string $client,array $param)
    {
        $res = $this->validateCreate($cid,$mn_sid,$client,$param);
        if ($res['code'] != Normal::SUC) {
            return $res;
        }
        
        $_current_time  = time();
        $account        = $param['mobile'];
        $password       = $param['password'];
        $nickname       = $param['nickname'] ?? '';
        $avatar         = $param['img'] ?? '';
        $guide_code     = $param['guide_code'] ?? '';
        
        $_insert_data  = [
            'account'    => $account,
            'password'   => md5($password),
            'cd'         => $_current_time,
            'client'     => $client,
            'mn_sid'     => $mn_sid,
            'cid'        => $cid,
        ];
        
        $member_id = Member::instance()->add($_insert_data);
        if( !$member_id ) {
            return Normal::returnCode(Normal::FAIL,"注册失败，请稍后再试100000！");
        }
        
        $_member_info_data = [
            'mid'       => $member_id,
            'nickname'  => !empty($nickname) ? $nickname : $this->hide_mobile($account),
            'avatar'    => $avatar,
        ];
        
        //如果有导购码
        if(!empty($guide_code))
        {
            $where_guide = [
                'code'      => trim($guide_code),
                'mn_sid'    => $mn_sid,
                'cid'       => $cid,
                'status'    => 1
            ];
            
            $_guide_info = ShoppingGuide::instance()->info($where_guide);
            if( !empty($_guide_info) )
            {
                $_guide_id = $_guide_info['id'];
                $_member_info_data['guide_id']  = $_guide_id;
                $_member_info_data['guide_now'] = $_guide_id;
                $_member_info_data['guide_cd']  = $_current_time;
                $_member_info_data['guide_md']  = $_current_time;
                MemberGuideLog::instance()->add([
                    'mid'       =>  $member_id,
                    'guide_id'  =>  $_guide_id,
                    'cd'        =>  $_current_time
                ]);
            }
        }
        
        $member_info_id = MemberInfo::instance()->insert($_member_info_data);
        if( ! $member_info_id ) {
            return Normal::returnCode(Normal::FAIL,"注册失败，请稍后再试100001！");
        }
        
        return Normal::returnCode(Normal::SUC,'创建成功',[
            'member_id'         => $member_id,
        ]);
    }
    
    /**
     * 账户创建验证
     * @param int $cid
     * @param int $mn_sid
     * @param string $client
     * @param array $param
     * @return int[]|string[]|array[]
     */
    public function validateCreate(int $cid,int $mn_sid,string $client,array $param)
    {
        if ( empty($param['mobile'])) {
            return Normal::returnCode(Normal::FAIL,'账号不能为空');
        }
        
        $where = [
            'account'   => $param['mobile'],
            'mn_sid'    => $mn_sid,
            'cid'       => $cid,
        ];
        
        $member= Member::instance()->info($where);
        if( !empty($param['guide_code'])) 
        {
            $info = ShoppingGuide::instance()->info(['code'=>trim($param['guide_code']),'mn_sid'=>$mn_sid,'cid'=>$cid]);
            if(empty($info)){
                return Normal::returnCode(Normal::FAIL,'导购员不存在');
            }
            
            if($info['status']==0){
                return Normal::returnCode(Normal::FAIL,'导购员已禁用');
            }
        }
        
        if (!empty($member)) {
            return Normal::returnCode(Normal::FAIL,'账号已经存在');
        }
    
        if (empty($param['password'])) {
            return Normal::returnCode(Normal::FAIL,'密码不能为空');
        }
        
        return Normal::returnCode(Normal::SUC,'验证通过');
    }
    
    /**
     * 处理手机号码
     * @param string $mobile
     * @return mixed
     */
    public function hide_mobile(string $mobile = '')
    {
        return str_replace(substr($mobile, 3, 4), '****', $mobile);
    }
}